package com.dmgis.qxfw.dataservice.bigdata.controller;

import com.dmgis.qxfw.dataservice.bigdata.service.InfoDisplayService;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.xt.collect_log_monitor;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.yj.warn_signal_info;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.WarnSignData;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/xtsj")
public class InfoDisplayController {

    @Resource
    private InfoDisplayService service;

    @RequestMapping(value = "/CollectNum", method = RequestMethod.GET)
    @ResponseBody
    public ArrayList getCollectNumByTimeRange() throws Exception {

        // 接口
        List<collect_log_monitor> datalist = service.getCollectNumByTimeRange();
        String[] result0 = new String[datalist.size()];
        int[] result1 = new int[datalist.size()];
        ArrayList result = new ArrayList();
        if (datalist != null) {
            String name = "";
            Integer value = 0;
            int j=0;
            for (Integer i=datalist.size()-1;i>=0;i--) {
                if (datalist.get(i).getName().equals("ER03")){
                    name="雨量";
                }else if(datalist.get(i).getName().equals("LCC")) {
                    name="低云量";
                } else if(datalist.get(i).getName().equals("TCC")) {
                    name="总云量";
                } else if(datalist.get(i).getName().equals("10uv.u")) {
                    name="十米风";
                }
                else{
                    name = datalist.get(i).getName();
                }
                value = datalist.get(i).getRemark();
                result0[j]=name;
                result1[j]=value;
                j++;
            }
            result.add(result0);
            result.add(result1);
        }
        return result;
    }

    @RequestMapping(value = "/MonitorNum", method = RequestMethod.GET)
    @ResponseBody
    public Object getMonitorNumByTimeRange() throws Exception {

        // 接口
        List<Map<String, Object>> datalist = service.getMonitorNumByTimeRange();
        Map<String, Object>[]  result=new Map[datalist.size()];
        for (int i=0;i<datalist.size();i++)
        {
            result[i]=datalist.get(i);

        }
        return result;
    }

    @RequestMapping(value = "/WarningSign", method = RequestMethod.GET)
    @ResponseBody
    public List<WarnSignData> getWarningSignByMonth(HttpServletRequest requst) throws Exception {
        // 接口
       // int count=0;
        List<WarnSignData> result=new ArrayList<>();
        String where=requst.getParameter("date");
        List<warn_signal_info> datalist = service.getWarningSignByMonth(where);

        if (datalist.size()>0) {
            String type = "";
            String level = "";
            String day;
            Map<String,ArrayList<String>> warnMap = new HashMap<>();
            for (int i=0;i<datalist.size();i++) {
                day = new DateTime(datalist.get(i).getAnnouncedtime()).GetDay()+"";
//                day=datalist.get(i).getAnnouncedtime()
                type = datalist.get(i).getType();
                level = datalist.get(i).getColor();
               if (warnMap.containsKey(day)){
                   warnMap.get(day).add(type+level);
               }
               else {
                   ArrayList<String> typecolor=new ArrayList<>();
                   typecolor.add(type+level);
                   warnMap.put(day,typecolor);
               }
            }
            for(String key :warnMap.keySet()){
                WarnSignData signData=new WarnSignData();
                signData.setDay(Integer.valueOf(key));
                Collections.sort(warnMap.get(key),new SortByLevel());
                signData.setTypecolor(warnMap.get(key));
                result.add(signData);
            }
        }
        return result;
    }

    @RequestMapping(value = "/getTemp", method = RequestMethod.GET)
    @ResponseBody
    public Object getskPoint() throws Exception {
        List<msgmediumsmallscale> resultList = service.getskPoint();
        List<String> hours=new ArrayList<>();
        List<Float> rain=new ArrayList<>();
        List<Float> temp=new ArrayList<>();
        List<Object> result=new ArrayList<>();
        int index=resultList.size();
        for (int i=0;i<index;i+=2){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH");
            String temptime=simpleDateFormat.format(resultList.get(i).getObservtime());
            hours.add(temptime);
            rain.add(resultList.get(i).getRain());
            temp.add(resultList.get(i).getDrybulbtemp());
        }
        result.add(hours);
        result.add(rain);
        result.add(temp);
        return result;
    }

    @RequestMapping(value = "/getCollectNum", method = RequestMethod.GET)
    @ResponseBody
    public Object getCollectNum() throws Exception {
        List<Map<String, Object>> resultList = service.getCollectNum();//行业
        Map<String, Object>[]  result=new Map[resultList.size()];
        for (int i=0;i<resultList.size();i++)
        {
            result[i]=resultList.get(i);
        }
        return result;
    }

    @RequestMapping(value = "/ProcuctCount", method = RequestMethod.GET)
    @ResponseBody
    public Object GetProductCount(){
        Object o = service.GetProductCount();
        return o;
    }
}

class SortByLevel implements Comparator<String> {
    public int compare(String lev1, String lev2) {
        return levName2Code(lev2).compareTo(levName2Code((lev1)));
    }
    private   String levName2Code(String typecolor){
        String color =typecolor.substring(typecolor.length()-2,typecolor.length());
        String levName="0";
        switch (color){
            case "蓝色":
                levName="0";
                break;
            case "黄色":
                levName="1";
                break;
            case "橙色":
                levName="2";
                break;
            case "红色":
                levName="3";
                break;
        }
        return levName;
    }
}